layui.config({
  base: '/src/'
}).use('index', function () {
  var $ = layui.$
    , admin = layui.admin
    , main = layui.main
    , setter = layui.setter
    , layer = layui.layer
    , dropdown = layui.dropdown
    , form = layui.form
    , table = layui.table;


  var pid = "0";
  pid = main.getPageParams("pid");
  if (pid == null) pid = "0"; 

  var jcxmflag = main.getPageParams("jcxmflag");
  if (jcxmflag == null) {
    // if (qytype == "1")
    //   jcxmflag = "J10";
    // else if (qytype == "2")
    //   jcxmflag = "J20";
    // else if (qytype == "3")
      jcxmflag = "J10";
  }

  loadxmlx(jcxmflag);
  loadJCMXPath(pid, jcxmflag);
  function loadxmlx( jcxmflag) {
    //初始化检查项目类型
    admin.req({
      url: 'system/UtilityTable/Query'
      , data: { utype: 'JCXMLX'  }
      , type: 'get'
      , done: function (res) {
        debugger
        var res = res.data;
        if (res != null) {
          $.each(res, function (index, item) {
            debugger
            $('#jcxmflag').append(new Option(item.uname, item.value));
          });
          $("#jcxmflag").val(jcxmflag);
          form.render("select");

        }
      }
    });
  }

  function loadJCMXPath(pid, jcxmflag) {
    //初始化检查项目路径
    admin.req({
      url: 'jdjc/jcxm/GetPath'
      , type: 'get'
      , data: { id: pid, jcflag: jcxmflag, pid: pid }
      , done: function (res) {
        $("#divpath").html(res.data);
        $('.layuiadmin-btn-jcxm-list').on('click', function () {
          var type = $(this).data('type');
          var pid = $(this)[0].id;
          var jclx = $("#jcxmflag").val();
          location.href = '/src/views/system/jcxm/yj_jdjc_jcxm_list.html?pid=' + pid + '&jcxmflag=' + jclx  ;
        });
      }
    });
  }

  //加载列表
  table.render({
    elem: '#LAY-app-jcxm-list'
    , url:  setter.serverAdd +   'jdjc/jcxm/Query' 
    ,where:{parentid:pid,jcxmflag:jcxmflag}
    , title: '数据表'
    , toolbar: '#toolbarDemo'
    , page: true
    , cols: [[
      { type: 'checkbox', fixed: 'left' }
      , { field: 'itemnumber', title: '检查项目编号', width: 120 }
      , { field: 'itemname', title: '检查项目名称' }
      , { field: 'paixu', title: '排序', width: 80 }
      , { field: 'isvoteitem', title: '是否否决项', width: 100, toolbar: '#voteitem' }
      , { title: '下一级', width: 100, align: 'center', fixed: 'right', toolbar: '#nextlever' }
      , { title: '操作', width: 140, align: 'center', fixed: 'right', toolbar: '#LAY-app-jcxm-toolbar' }

    ]]
    , done: function (res) {
      dropdown.render({
        elem: '.rowMode',
        data: [
          { id: 'default-row', title: '单行模式（默认）' }
          , { id: 'multi-row', title: '多行模式' }
        ],
        // 菜单被点击的事件
        click: function (obj) {
          switch (obj.id) {
            case 'default-row':
              table.reload('LAY-app-jcxm-list', { lineStyle: null });//恢复单行
              break;
            case 'multi-row':
              table.reload('LAY-app-jcxm-list', { lineStyle: 'height: 95px;' });
              break;
          }
        }
      });
    }
  });


  //查询
  form.on('submit(table-search)', function (data) {
    var field = data.field;
    data.field.parentid = '0';
    pid = "0";
    jcxmflag = data.field.jcxmflag
    debugger
    table.reload('LAY-app-jcxm-list', { page: { curr: 1 }, where: field });
    loadJCMXPath('0', jcxmflag)
    return false; //阻止默认form 跳转
  });

  //工具栏事件（添加）
  table.on('toolbar(LAY-app-jcxm-list)', function (obj) {
    var options = obj.config; //获取当前表格属性配置项
    var checkStatus = table.checkStatus(options.id); //获取选中行相关数据
    // 根据不同的事件名进行相应的操作
    switch (obj.event) { //对应模板元素中的 lay-event 属性值
      case 'add':
        layui.data(setter.tableName, { key: 'jcxmpid', value: pid });
        layui.data(setter.tableName, { key: 'jcxmflag', value: jcxmflag });
        layer.open({
          type: 2,
          title: '添加信息',
          shadeClose: true,
          maxmin: true, //开启最大化最小化按钮
          area: ['70%', '90%'],
          content: setter.views + 'system/jcxm/yj_jdjc_jcxm_add.html?jcxmflag=J10',
          end: function () {
            table.reload('LAY-app-jcxm-list'); //刷新表格
          }
        });
        break;
    };
  });

  //单元格工具事件 (编辑&更多)
  table.on('tool(LAY-app-jcxm-list)', function (obj) {
    var data = obj.data; //获得当前行数据
    if (obj.event === 'edit') {
      layer.open({
        type: 2,
        title: '编辑',
        shadeClose: true,
        maxmin: true,
        area: ['70%', '90%'],
        content: setter.views + 'system/jcxm/yj_jdjc_jcxm_edit.html?id=' + data.id,
        end: function () {
          table.reload('LAY-app-jcxm-list'); //刷新表格
        }
      });
    }
    else if (obj.event === 'more') {//下拉菜单-更多
      dropdown.render({
        elem: this, //触发事件的 DOM 对象
        show: true, //外部事件触发即显示
        data: [
          { title: '详情', id: 'see' }
          , { title: '删除', id: 'del' }
        ]
        , click: function (menudata) {
          if (menudata.id === 'see') {
            layer.open({
              type: 2,
              title: '详情',
              shadeClose: true,
              maxmin: true,
              area: ['60%', '80%'],
              content: setter.views + 'system/jcxm/yj_jdjc_jcxm_info.html?id=' + data.id
            });
          }
          else if (menudata.id === 'del') {
            layer.confirm('确认删除 ?', function (index) {
              admin.req({
                url: 'jdjc/jcxm/Delete'
                , type: 'delete'
                , data: { id: data.id }
                , done: function (res) {
                  if (res.successful) {
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                  }
                  admin.msg_success(res.msg);
                }
              });
            });
          }
        },
        align: 'right', //右对齐弹出
        style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
      });
    }
    else if (obj.event == 'next') {
      var parentid = data.id;
      var jclx = data.jcxmflag;
      location.href = '/src/views/system/jcxm/yj_jdjc_jcxm_list.html?pid=' + parentid + '&jcxmflag=' + jclx ;
    }
  });

});